package com.agger.harmonyosthemeseditor.userinterface;

import com.agger.harmonyosthemeseditor.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.system.version.SystemVersion;

public class GeneralToast {

    private Context mContext;
    private String mText;
    private static ToastDialog LastToast = null;

    public GeneralToast(Context context) {
        mContext = context;
    }

    public GeneralToast setText(String text)
    {
        mText = text;
        return this;
    }

    public void show()
    {
        //API4之前仅涉及智慧屏、手表等设备，不包含平板和手机，API4是首个支持手机和平板的鸿蒙2.0，API5为鸿蒙2.1，API6为鸿蒙2.2，API7为鸿蒙3.0
        //事实上，大部分鸿蒙2.0的手机或平板，早已换作API5鸿蒙2.1的内核，仅仅只是UI还停留在2.0，因此华为分析后台会将这些API5的设备识别为鸿蒙2.1，而系统关于界面却仍然是鸿蒙2.0
        int API_Version_Code = SystemVersion.getApiVersion();
        try { if(LastToast != null) LastToast.destroy(); } catch (Exception ex) {ex.printStackTrace();}
        DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(mContext)
                .parse(ResourceTable.Layout_general_toast, null, false);
        Text ToastText = (Text)toastLayout.findComponentById(ResourceTable.Id_msg_toast);
        if(mText.contains("\r\n"))
        {
            String[] TextLines = mText.split("\r\n");
            int maxWidth = 0;
            int num = 0;
            for(int i = 0;i < TextLines.length;i++)
            {
                int mWidth = TextLines[i].length();
                if(mWidth > maxWidth)
                {
                    maxWidth = mWidth;
                    num = i;
                }
            }
            toastLayout.setVisibility(Component.INVISIBLE);
            ToastText.setText(TextLines[num]);
        }
        else ToastText.setText(mText);

        ToastDialog toastDialog = new ToastDialog(mContext);
        LastToast = toastDialog;
        if(API_Version_Code < 6)
        {
            toastDialog
                    .setContentCustomComponent(toastLayout)
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT)
                    .setAlignment(LayoutAlignment.BOTTOM)
                    .setTransparent(true)
                    .show();
        }
        else
        {
            toastLayout.findComponentById(ResourceTable.Id_ToastBox).setHeight(DirectionalLayout.LayoutConfig.MATCH_CONTENT);
            toastLayout.findComponentById(ResourceTable.Id_ToastBox).setWidth(DirectionalLayout.LayoutConfig.MATCH_CONTENT);
            toastDialog
                    .setContentCustomComponent(toastLayout)
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setAlignment(LayoutAlignment.BOTTOM)
                    .setTransparent(true)
                    .show();
        }

        if(mText.contains("\r\n"))
        {
            DirectionalLayout _toastLayout = (DirectionalLayout) LayoutScatter.getInstance(mContext)
                    .parse(ResourceTable.Layout_general_toast, null, false);
            Text _ToastText = (Text)_toastLayout.findComponentById(ResourceTable.Id_msg_toast);
            _ToastText.setWidth(ToastText.getWidth());
            _ToastText.setText(mText);
            toastDialog.destroy();
            LastToast = new ToastDialog(mContext);
            if(API_Version_Code < 6)
            {
                LastToast
                        .setContentCustomComponent(_toastLayout)
                        .setSize(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT)
                        .setAlignment(LayoutAlignment.BOTTOM)
                        .setTransparent(true)
                        .show();
            }
            else
            {
                _toastLayout.findComponentById(ResourceTable.Id_ToastBox).setHeight(DirectionalLayout.LayoutConfig.MATCH_CONTENT);
                _toastLayout.findComponentById(ResourceTable.Id_ToastBox).setWidth(DirectionalLayout.LayoutConfig.MATCH_CONTENT);
                LastToast
                        .setContentCustomComponent(_toastLayout)
                        .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                        .setAlignment(LayoutAlignment.BOTTOM)
                        .setTransparent(true)
                        .show();
            }

        }
    }

    public void showOnTop()
    {
        //API4之前仅涉及智慧屏、手表等设备，不包含平板和手机，API4是首个支持手机和平板的鸿蒙2.0，API5为鸿蒙2.1，API6为鸿蒙2.2，API7为鸿蒙3.0
        //事实上，大部分鸿蒙2.0的手机或平板，早已换作API5鸿蒙2.1的内核，仅仅只是UI还停留在2.0，因此华为分析后台会将这些API5的设备识别为鸿蒙2.1，而系统关于界面却仍然是鸿蒙2.0
        int API_Version_Code = SystemVersion.getApiVersion();
        try { if(LastToast != null) LastToast.destroy(); } catch (Exception ex) {ex.printStackTrace();}
        DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(mContext)
                .parse(ResourceTable.Layout_general_toast_top, null, false);
        Text ToastText = (Text)toastLayout.findComponentById(ResourceTable.Id_msg_toast);
        if(mText.contains("\r\n"))
        {
            String[] TextLines = mText.split("\r\n");
            int maxWidth = 0;
            int num = 0;
            for(int i = 0;i < TextLines.length;i++)
            {
                int mWidth = TextLines[i].length();
                if(mWidth > maxWidth)
                {
                    maxWidth = mWidth;
                    num = i;
                }
            }
            toastLayout.setVisibility(Component.INVISIBLE);
            ToastText.setText(TextLines[num]);
        }
        else ToastText.setText(mText);

        ToastDialog toastDialog = new ToastDialog(mContext);
        LastToast = toastDialog;
        if(API_Version_Code < 6)
        {
            toastDialog
                    .setContentCustomComponent(toastLayout)
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT)
                    .setAlignment(LayoutAlignment.TOP)
                    .setTransparent(true)
                    .show();
        }
        else
        {
            toastLayout.findComponentById(ResourceTable.Id_ToastBox).setHeight(DirectionalLayout.LayoutConfig.MATCH_CONTENT);
            toastLayout.findComponentById(ResourceTable.Id_ToastBox).setWidth(DirectionalLayout.LayoutConfig.MATCH_CONTENT);
            toastDialog
                    .setContentCustomComponent(toastLayout)
                    .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setAlignment(LayoutAlignment.TOP)
                    .setTransparent(true)
                    .show();
        }

        if(mText.contains("\r\n"))
        {
            DirectionalLayout _toastLayout = (DirectionalLayout) LayoutScatter.getInstance(mContext)
                    .parse(ResourceTable.Layout_general_toast_top, null, false);
            Text _ToastText = (Text)_toastLayout.findComponentById(ResourceTable.Id_msg_toast);
            _ToastText.setWidth(ToastText.getWidth());
            _ToastText.setText(mText);
            toastDialog.destroy();
            LastToast = new ToastDialog(mContext);
            if(API_Version_Code < 6)
            {
                LastToast
                        .setContentCustomComponent(_toastLayout)
                        .setSize(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT)
                        .setAlignment(LayoutAlignment.TOP)
                        .setTransparent(true)
                        .show();
            }
            else
            {
                _toastLayout.findComponentById(ResourceTable.Id_ToastBox).setHeight(DirectionalLayout.LayoutConfig.MATCH_CONTENT);
                _toastLayout.findComponentById(ResourceTable.Id_ToastBox).setWidth(DirectionalLayout.LayoutConfig.MATCH_CONTENT);
                LastToast
                        .setContentCustomComponent(_toastLayout)
                        .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                        .setAlignment(LayoutAlignment.TOP)
                        .setTransparent(true)
                        .show();
            }

        }
    }
}
